package ebusiness.ebusiness.Dao.UserDao;

import ebusiness.ebusiness.Dao.DBManager;
import ebusiness.ebusiness.Entity.Address;
import ebusiness.ebusiness.Entity.UserEntity.RegisteUser;
import ebusiness.ebusiness.Entity.UserEntity.User;
import ebusiness.ebusiness.Entity.UserEntity.UserDetail;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.session.SqlSession;


import java.util.List;

public class UserQueryVO {
    //包括地址管理
    public  int GeneralAddressID() throws Exception{
        int aid =(int)(Math.random()*1352651);
        try {
            List<Integer> ids=getAllAddressID();
            if(ids.size()>0){
                for(int i=0;i<100;i++){
                    if(ids.contains(aid)){
                        aid=(int)(Math.random()*1352651);
                    } else{
                        return aid;
                    }
                }

            }else return aid+1;

        }catch (Exception e){
            throw e;
        }


        return -3;
    }

    public List<Integer> getAllAddressID()throws Exception{
        SqlSession sqlSession=DBManager.GetSqlFactory().openSession();
        try {
            UserQuery orderInter=sqlSession.getMapper(UserQuery.class);
            List<Integer> orderList= orderInter.getAllAddressID();
            sqlSession.close();
            return orderList;
        }catch (Exception e){
            e.printStackTrace();
            sqlSession.close();
        }return null;

    }
    public Address getAddressById(int addressId) throws Exception{
        SqlSession sqlSession=DBManager.GetSqlFactory().openSession();
        try {
            UserQuery userQuery=sqlSession.getMapper(UserQuery.class);
            userQuery.getAddressById(addressId);
            sqlSession.close();
            return userQuery.getAddressById(addressId);
        }catch (Exception e){
            e.printStackTrace();
            sqlSession.close();
        }return null;

    }
    public List<Address> getAddressByUser(int userID)throws Exception{
        SqlSession sqlSession=DBManager.GetSqlFactory().openSession();
        try {
            UserQuery userQuery=sqlSession.getMapper(UserQuery.class);
            List<Address> ads= userQuery.getAddressByUser(userID);
            sqlSession.close();
            return ads;
        }catch (Exception e){
            e.printStackTrace();
            sqlSession.close();
        }return null;

    }
    public int addAddress(Address address)throws Exception{
        SqlSession sqlSession=DBManager.GetSqlFactory().openSession();
        UserQuery userQuery=sqlSession.getMapper(UserQuery.class);
        try {
        userQuery.addAddress(address.getAddress_id(),address.getCustomer_name(),address.getAddress_province(),
                address.getAddress_city(),address.getAddress(),address.getAddress_postal(),
                address.getCustomer_id());
        sqlSession.commit();
        sqlSession.close();
            return address.getAddress_id();
        }catch (Exception e){
            e.printStackTrace();
            sqlSession.close();
            return -2;
        }

    }
    public int removeAddress(int id)throws Exception{
        SqlSession sqlSession=DBManager.GetSqlFactory().openSession();
        UserQuery userQuery=sqlSession.getMapper(UserQuery.class);
        try {
            userQuery.deladdress(id);
            sqlSession.commit();
            sqlSession.close();
            return id;
        }catch (Exception e){
            e.printStackTrace();
            sqlSession.close();
            return -2;
        }


    }
    public int modifyAddress(Address address)throws Exception{

        SqlSession sqlSession=DBManager.GetSqlFactory().openSession();
        UserQuery userQuery=sqlSession.getMapper(UserQuery.class);
        try {
            userQuery.modifyAddress(address.getAddress_id(),address.getCustomer_name(),address.getAddress_province(),
                    address.getAddress_city(),address.getAddress(),address.getAddress_postal(),
                    address.getCustomer_id());
                sqlSession.close();
            return address.getAddress_id();
        }catch (Exception e) {
            sqlSession.close();
            e.printStackTrace();
            return -2;
        }

    }


    public RegisteUser getRegisteUserByID(int id) throws Exception{
       SqlSession sqlSession=DBManager.GetSqlFactory().openSession();
        try {
            UserQuery userQuery=sqlSession.getMapper(UserQuery.class);
            RegisteUser user=userQuery.getRegisteUserByID(id);
            sqlSession.close();
            return user;
        }catch (Exception e){
            e.printStackTrace();
            sqlSession.close();
        }return null;

    }
    public RegisteUser getRegisteUserByName(String name) throws Exception{
        SqlSession sqlSession = DBManager.GetSqlFactory().openSession();
        try {
            UserQuery userQuery = sqlSession.getMapper(UserQuery.class);
            RegisteUser user=userQuery.findRegistUserByName(name);
            sqlSession.close();
            return user;
        }catch (Exception e){
            sqlSession.close();
            e.printStackTrace();
        }return null;

    }
    public int addARegisteUser(RegisteUser user)throws Exception{
    	
        return addARegisteUser(user.getName(),user.getPassword());
    }

    public int modifyPersonInfo(UserDetail personInfo) throws  Exception{
        SqlSession sqlSession = DBManager.GetSqlFactory().openSession();
        try{
            UserQuery userQuery=sqlSession.getMapper(UserQuery.class);
           userQuery.modifyPersonInfo(personInfo.getName(),personInfo.getId(),personInfo.getId(),personInfo.getCustomer_email(),
                   personInfo.getCustomer_phone(),personInfo.getCustomer_sex(),personInfo.getCustomer_birth(),
                   personInfo.getCustomer_hobby(),personInfo.getCustomer_num(),personInfo.getCustomer_marrige(),
                   personInfo.getCustomer_education(),personInfo.getCustomer_mincome(),personInfo.getCustomer_job(),
                   personInfo.getCustomer_img());
            sqlSession.commit();
            sqlSession.close();
            return 1;
        }catch (Exception e ){
            e.printStackTrace();
            sqlSession.close();
            return 0;
        }

    }
    public int addPersonInfo(UserDetail personInfo) throws Exception{
        SqlSession sqlSession = DBManager.GetSqlFactory().openSession();
        try{
            UserQuery userQuery=sqlSession.getMapper(UserQuery.class);


            userQuery.addPersonInfo(personInfo.getName(),personInfo.getId(),personInfo.getId(),personInfo.getCustomer_email(),
                    personInfo.getCustomer_phone(),personInfo.getCustomer_sex(),personInfo.getCustomer_birth(),
                    personInfo.getCustomer_hobby(),personInfo.getCustomer_num(),personInfo.getCustomer_marrige(),
                    personInfo.getCustomer_education(),personInfo.getCustomer_mincome(),personInfo.getCustomer_job(),
                    personInfo.getCustomer_img());
            sqlSession.commit();
            sqlSession.close();
            return 1;
        }catch (Exception e ){
            e.printStackTrace();
            sqlSession.close();
            return 0;
        }

    }

    public int modifyPassWord(int uid,String newp) throws Exception{
        SqlSession sqlSession = DBManager.GetSqlFactory().openSession();
        try{
            UserQuery userQuery=sqlSession.getMapper(UserQuery.class);
            userQuery.modifyPassWord(uid,newp);
            sqlSession.commit();
            sqlSession.close();
            return uid;
        }catch (Exception e ){
            e.printStackTrace();
            sqlSession.close();
            return -1;
        }
    }
    public UserDetail getUserDetailByID(int id) throws Exception{
        SqlSession sqlSession=DBManager.GetSqlFactory().openSession();
        try {
            UserQuery userQuery=sqlSession.getMapper(UserQuery.class);
            UserDetail user=userQuery.getUserDetailByID(id);
            sqlSession.close();
            return user;
        }catch (Exception e){
            sqlSession.close();
            e.printStackTrace();
        }return null;

    }
    public int addARegisteUser(String customer_name,String customer_password)throws  Exception{

        SqlSession sqlSession = DBManager.GetSqlFactory().openSession();
        try{
            UserQuery userQuery=sqlSession.getMapper(UserQuery.class);
            userQuery.addARegisteUser( customer_name, customer_password);
            sqlSession.commit();
            sqlSession.close();
            return 1;
        }catch (Exception e ){
        	System.out.println(e);
        	sqlSession.close();
            return -1;
        }

    }
}
